package common.server.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import common.server.http.NameValuePair;


/**
 * Http请求帮助类
 * 
 */
public class HttpUtils {

	public static final int CONNECT_TIMEOUT = 3000;
	public static final int READ_TIMEOUT = 10000;

	/**
	 * 使用get方式发送
	 * 
	 * @param url
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @return
	 */
	public static String get(String url, String responseEncoding) {
		return get(url, responseEncoding, CONNECT_TIMEOUT, READ_TIMEOUT);
	}

	/**
	 * 使用get方式发送
	 * 
	 * @param url
	 * @param headers
	 *            请求头
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String get(String url, Map<String, String> headers, String responseEncoding, int connectTimeout, int readTimeout) {
		HttpClient client = new HttpClient(null, null, connectTimeout, readTimeout);
		HttpResponse response = client.get(url, headers);
		try {
			return HttpTools.convertToString(response, true, responseEncoding);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

	/**
	 * 使用get方式发送
	 * 
	 * @param url
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String get(String url, String responseEncoding, int connectTimeout, int readTimeout) {
		return get(url, null, responseEncoding, connectTimeout, readTimeout);
	}

	/**
	 * 使用get方式发送
	 * 
	 * @param url
	 * @param parameters
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String get(String url, List<NameValuePair<String>> parameters, String requestEncoding, String responseEncoding, int connectTimeout, int readTimeout) {
		String u = HttpTools.getRequestUrl(url, parameters, requestEncoding);
		return get(u, responseEncoding, connectTimeout, readTimeout);
	}

	/**
	 * 使用get方式发送
	 * 
	 * @param url
	 * @param parameters
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String get(String url, Map<String, String> parameters, String requestEncoding, String responseEncoding, int connectTimeout, int readTimeout) {

		List<NameValuePair<String>> paramList = new ArrayList<NameValuePair<String>>();
		if (parameters != null) {
			for (String key : parameters.keySet()) {
				paramList.add(new NameValuePair<String>(key, parameters.get(key)));
			}
		}
		return get(url, paramList, requestEncoding, responseEncoding, connectTimeout, readTimeout);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @return
	 */
	public static String post(String url, String contentType, String parameters, Map<String, String> headers, String requestEncoding, String responseEncoding) {
		return post(url, contentType, parameters, headers, requestEncoding, responseEncoding, CONNECT_TIMEOUT, READ_TIMEOUT);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String post(String url, String contentType, String parameters, Map<String, String> headers, String requestEncoding, String responseEncoding, int connectTimeout, int readTimeout) {

		if (StringUtils.isNotEmpty(requestEncoding)) {
			requestEncoding = "utf-8";
		}
		if (StringUtils.isNotEmpty(responseEncoding)) {
			responseEncoding = "utf-8";
		}

		if (StringUtils.isEmpty(contentType)) {
			contentType = HttpClient.FORM_URLENCODED;
		}

		if (connectTimeout <= 0) {
			connectTimeout = CONNECT_TIMEOUT;
		}
		if (readTimeout <= 0) {
			readTimeout = READ_TIMEOUT;
		}

		HttpClient client = new HttpClient(null, null, connectTimeout, readTimeout);
		HttpResponse response = client.post(url, headers, contentType, parameters, requestEncoding);
		try {
			return HttpTools.convertToString(response, true, responseEncoding);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @return
	 */
	public static String post(String url, String contentType, List<NameValuePair<String>> parameters, Map<String, String> headers, String requestEncoding, String responseEncoding) {
		return post(url, contentType, parameters, headers, requestEncoding, responseEncoding, CONNECT_TIMEOUT, READ_TIMEOUT);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String post(String url, String contentType, List<NameValuePair<String>> parameters, Map<String, String> headers, String requestEncoding, String responseEncoding, int connectTimeout, int readTimeout) {

		if (StringUtils.isEmpty(requestEncoding)) {
			requestEncoding = "utf-8";
		}

		StringBuilder sb = new StringBuilder();

		if (parameters != null) {
			for (NameValuePair<String> pair : parameters) {
				if (sb.length() > 0) {
					sb.append("&");
				}
				sb.append(pair.getKey());
				sb.append("=");
				sb.append(pair.getValue());
			}
		}

		return post(url, contentType, sb.toString(), headers, requestEncoding, responseEncoding, connectTimeout, readTimeout);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @return
	 */
	public static String post(String url, String contentType, Map<String, String> parameters, Map<String, String> headers, String requestEncoding, String responseEncoding) {

		List<NameValuePair<String>> paramList = new ArrayList<NameValuePair<String>>();
		if (parameters != null) {
			for (String key : parameters.keySet()) {
				paramList.add(new NameValuePair<String>(key, parameters.get(key)));
			}
		}
		return post(url, contentType, paramList, headers, requestEncoding, responseEncoding);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param contentType
	 *            可为NULL,默认 application/x-www-form-urlencoded
	 * @param parameters
	 *            可为NULL
	 * @param headers
	 *            可为NULL
	 * @param requestEncoding
	 *            可为NULL,默认 utf-8
	 * @param responseEncoding
	 *            可为NULL,默认 utf-8
	 * @param connectTimeout
	 *            小于等于0则取默认值 CONNECT_TIMEOUT
	 * @param readTimeout
	 *            小于等于0则取默认值 READ_TIMEOUT
	 * @return
	 */
	public static String post(String url, String contentType, Map<String, String> parameters, Map<String, String> headers, String requestEncoding, String responseEncoding, int connectTimeout, int readTimeout) {

		List<NameValuePair<String>> paramList = new ArrayList<NameValuePair<String>>();
		if (parameters != null) {
			for (String key : parameters.keySet()) {
				paramList.add(new NameValuePair<String>(key, parameters.get(key)));
			}
		}
		return post(url, contentType, paramList, headers, requestEncoding, responseEncoding, connectTimeout, readTimeout);
	}

	/**
	 * 使用post方式发送
	 * 
	 * @param url
	 * @param parameters
	 *            可为NULL
	 * @param encoding
	 *            可为NULL,默认 utf-8
	 * @return
	 */
	public static String post(String url, Map<String, String> parameters, String encoding) {
		return post(url, null, parameters, null, encoding, encoding);
	}

}
